<?php

namespace api\modules\v1\models;

use api\func\func;
use Yii;
use api\common\models\Common;
use api\modules\v1\models\Like;
use api\common\controllers\CommonController;
use api\common\models\Mar;
use yii\db\Expression;
use yii\helpers\Url;
use yii\data\Pagination;
use yii\web\Link;
use \yii\caching\FileDependency;
use api\modules\v1\models\Code;
class Activityapply extends Common
{
    public $code = '';
    /**
     * @return array|string
     */
    public static function tableName()
    {
        return '{{%user_activity_apply}}';
    }
    public function rules()
    {
        return [
            ['actid', 'required','message'=>'分类ID不能为空'],
            ['username', 'required'],
            ['username', 'string', 'max' => 10],
            ['email', 'required','message'=>'邮箱不能为空'],
            ['actid', 'integer','message'=>'分类ID必须是整数'],
            ['phone', 'required'],
            [['phone'],'match','pattern'=>'/^1[0-9]{10}$/'],
            [['email'],'email','message'=>'邮箱格式不正确'],
            ['notes', 'string', 'max' => 200,'message'=>'备注最长为200个字符'],
            ['notes', 'validateNotes'],
            [['code'], 'required','message'=>'验证码不能为空'],
            [['code'], 'filter', 'filter' => 'trim'],
            [['code'], 'validateCode'],
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'phone' => Yii::t('app', '手机号'),
            'username' => Yii::t('app', '用户名'),
        ];
    }

    /*
     * 备注非法过滤
     * @param $attribute
     * @param $params
     * @return bool
     */
    public function validateNotes($attribute, $params){
        if(func::safe_sql($this->notes)){
            $this->addError($attribute, '请勿非法提交!');
            return false;
        }
        return true;
    }

    /*
     * 验证 验证码
     * @param $attribute
     * @param $params
     * @return bool
     */
    public function validateCode($attribute, $params)
    {
        $Code = new Code();
        $data = $Code->validateCode($this->phone,$this->code);
        if($data['success'] == false){
            $this->addError($attribute, $data['msg']);
            return false;
        }
        return true;
    }
}
